﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Beetle.Sl.EditorControl.Core;

namespace Beetle.Sl.EditorControl.Controls
{
    public class FontSizeSelectPanel : Control
    {
        private SizeInfo[] _fontSizeLib;
        private SizeInfo selectSizeTemp = null;
        private ICommand _selectSizeCommand;

        public event EventHandler SelectedValue;

        public SizeInfo[] FontSizeLib
        {
            get
            {
                if (this._fontSizeLib == null)
                {
                    int[] sizeTemp = new int[] { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
                    this._fontSizeLib = new SizeInfo[sizeTemp.Length];

                    for (int i = 0; i < sizeTemp.Length; i++)
                    {
                        this._fontSizeLib[i] = new SizeInfo()
                        {
                            Size = sizeTemp[i],
                            Visibility = System.Windows.Visibility.Collapsed
                        };
                    }
                }

                return this._fontSizeLib;
            }
        }

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",
           typeof(int),
           typeof(FontSizeSelectPanel),
           new PropertyMetadata(8, new PropertyChangedCallback(FontSizeSelectPanel.OnValuePropertyChanged)));

        public int Value
        {
            get
            {
                return (int)this.GetValue(FontSizeSelectPanel.ValueProperty);
            }

            set
            {
                this.SetValue(FontSizeSelectPanel.ValueProperty, value);
            }
        }

        public ICommand SelectSizeCommand
        {
            get
            {
                if (this._selectSizeCommand == null)
                {
                    this._selectSizeCommand = new DelegateCommand(this.OnSelectSizeCommand);
                }

                return this._selectSizeCommand;
            }
        }

        private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        { }

        private void RaiseSelectedValue()
        {
            if (this.SelectedValue != null)
            {
                this.SelectedValue(this, null);
            }
        }

        public FontSizeSelectPanel()
        {
            this.DefaultStyleKey = typeof(FontSizeSelectPanel);
        }

        private void OnSelectSizeCommand(object arg)
        {
            if (this.selectSizeTemp != null)
            {
                if (this.selectSizeTemp != arg)
                {
                    this.selectSizeTemp.Visibility = System.Windows.Visibility.Collapsed;
                    this.selectSizeTemp = (SizeInfo)arg;
                    this.selectSizeTemp.Visibility = System.Windows.Visibility.Visible;
                }
            }
            else
            {
                this.selectSizeTemp = (SizeInfo)arg;
                this.selectSizeTemp.Visibility = System.Windows.Visibility.Visible;
            }

            SizeInfo sizeInfo = (SizeInfo)arg;
            this.Value = sizeInfo.Size;
            this.RaiseSelectedValue();
        }
    }
}
